package metis;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class MetisMain {
	public static String source;
	public static String binary;
	public HashMap<Integer, HashSet<Integer>> network;
	public static final boolean DEBUG = false;
	public ArrayList<Integer> clusterpath;

	public static void main(String[] args) throws IOException, InterruptedException {
		MetisMain main = new MetisMain();
		ArrayList<Integer> clusterp = new ArrayList<Integer>();
		main.run(args[0], args[1], clusterp, args[2]);

	}

	public HashMap<Integer, Integer> run(String source, String binary, ArrayList<Integer> clusterpath, String partitions) {
		MetisMain.source = source;
		MetisMain.binary = binary;
		String inputFileName;
		int parts = new Integer(partitions);
		MetisIO io = new MetisIO(source, binary, "test");
		MetisClustering clustering = new MetisClustering(source, binary);
		HashMap<Integer, Integer> clusterResult = new HashMap<Integer, Integer>();

		try {
			inputFileName = io.pairsToGraphFile();
			clustering.cluster(inputFileName, parts);
			clusterResult = io.getResults(inputFileName, parts);
			io.writeToFile(inputFileName, clusterResult);
			/* do the path to the clustering */
			for (int i = 0; i < clusterpath.size(); i++) {
				inputFileName = io.clusteringToGraphFile(clusterResult, i, clusterpath.get(i));
				MetisClustering clusteringPath = new MetisClustering(source, binary);
				clusteringPath.cluster(inputFileName, parts);
				clusterResult = io.getResults(inputFileName, parts);
				io.writeToFile(inputFileName, clusterResult);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return clusterResult;
	}

}
